//
//  YJWelcomeViewController.swift
//  YJWeiBo
//
//  Created by Jim1024 on 2017/1/11.
//  Copyright © 2017年 Jim1024. All rights reserved.
//

import UIKit
import SnapKit
import Kingfisher

class YJWelcomeViewController: UIViewController {

    // MARK: - lift cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        if let urlString = YJUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = URL(string: urlString)
            avatarView.kf.setImage(with: url, placeholder: UIImage(named: "avatar_default_big"))
        }
        
        // 获取用户的头像等信息,显示头像,需要等网络请求完了才加载
        YJUserAccountViewModel.sharedInstance.loadUserInfo { (error) in
            if error != nil {
                JPrint("请求用户信息出错了: \(error)")
                return
            }
            
            // 没有出错,给头像赋值,即使图片在本地有缓存也需要等到网络请求完了才设置
            if let urlString = YJUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
                let url = URL(string: urlString)
                self.avatarView.kf.setImage(with: url, placeholder: UIImage(named: "avatar_default_big"))
            }
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }

    private func setupUI() {
        // 添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(avatarView)
        view.addSubview(welcomeLabel)
        
        // layout
        
        bkgImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        avatarView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        welcomeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(avatarView)
            make.top.equalTo(avatarView.snp.bottom).offset(16)
        }
    }
    
    private func moveAnimation() {
        UIView.animate(withDuration: 1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { 
            self.avatarView.snp.updateConstraints({ (make) in
                make.bottom.equalToSuperview().offset(-(UIScreen.main.bounds.height - 160))
            })
            self.view.layoutIfNeeded()
        }) { (_) in
            UIView.animate(withDuration: YJDefaultDuration, animations: { 
                self.welcomeLabel.alpha = 1
            }, completion: { (_) in
                JPrint("动画完成")
                // 切换界面
                AppDelegate.outSwitchRootViewController(YJMainViewController())
            })
        }
    }

    // MARK: - 懒加载
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"));
    
    private lazy var avatarView: UIImageView = {
        let avatar = UIImageView(image: UIImage(named: "avatar_default_big"))
        avatar.layer.cornerRadius = 42.5
        avatar.layer.masksToBounds = true
        return avatar
    }()
    
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.font = UIFont.systemFont(ofSize: 14)
        label.alpha = 0.0
        return label
    }()

}
